package cn.lzj.pet.service.impl;

import cn.lzj.basic.service.IBaiduAiAuditService;
import cn.lzj.basic.service.impl.BaseServiceImpl;
import cn.lzj.basic.util.AjaxResult;
import cn.lzj.basic.util.LoginContext;
import cn.lzj.org.domain.Employee;
import cn.lzj.org.mapper.EmployeeMapper;
import cn.lzj.pet.domain.Pet;
import cn.lzj.pet.domain.PetDetail;
import cn.lzj.pet.domain.PetOnlineAuditLog;
import cn.lzj.pet.mapper.PetDetailMapper;
import cn.lzj.pet.mapper.PetMapper;
import cn.lzj.pet.mapper.PetOnlineAuditLogMapper;
import cn.lzj.pet.service.IPetService;
import cn.lzj.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void add(Pet pet) {
        super.add(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updatePetDetailByPetId(petDetail);
        }
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
        petDetailMapper.delPetDetailByPetId(id);
    }

    @Override
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLoginUser(request);
         Employee employee=employeeMapper.loadByLogininfoid(logininfo.getId());


        for (Long id:ids){
         Pet pet = petMapper.loadById(id);
         Boolean textBoo = baiduAiAuditService.textAudit(pet.getName());
         Boolean imageBoo = baiduAiAuditService.imageAudit(pet.getResources());
         if(textBoo&&imageBoo){
             pet.setOnsaletime(new Date());
             pet.setState(1);
             petMapper.update(pet);

             PetOnlineAuditLog log = new PetOnlineAuditLog();
             log.setPetId(pet.getId());
             log.setState(1);
             log.setAuditTime(new Date());
             log.setNote("上架成功！！");
             log.setAuditId(employee.getId());
             petOnlineAuditLogMapper.save(log);
         }else {
             PetOnlineAuditLog log = new PetOnlineAuditLog();
             log.setPetId(pet.getId());
             log.setState(-1);
             log.setAuditTime(new Date());
             log.setNote("上架失败，可能有非法内容！！！");
             log.setAuditId(employee.getId());
             petOnlineAuditLogMapper.save(log);
         }
     }


        return AjaxResult.util();
    }

    @Override
    public void offsale(List<Long> ids) {
        Date offsaletime = new Date();
        petMapper.offsale(offsaletime,ids);
    }
}
